package xyz.lisys.service.impl;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import xyz.lisys.domain.dao.CedUserDao;
import xyz.lisys.domain.po.CedUserPo;
import xyz.lisys.service.CedUserService;

import java.sql.SQLException;
import java.util.List;
import java.util.Random;

@Transactional
public class CedUserServiceImpl implements CedUserService {

    @Autowired
    private CedUserDao cedUserDao;

    @Autowired
    private CedUser2ServiceImpl userService2;

    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;

    @Override
    public List<CedUserPo> selectAll() {
        return cedUserDao.selectAll();
    }

    @Override
    public void insertUser() {
        CedUserPo user = new CedUserPo();
        user.setName("test");
        user.setPhone("21");
        user.setPassword("12");
        user.setGender("1");
        user.setIntro("211");
        user.setEmail("231");
        user.setAddress("21313");
        user.setIndustry("21553");
        user.setEdu("2443");
        user.setIsBoss("1");
        user.setCreateTime("0");
        user.setCreatePower(1);
        user.setUserHead("1231");
        cedUserDao.insertUser(user);
    }

    @Override
    public void insertUserException() {
        insertUser();
        int i = 1 / 0;
    }

    @Override
    public void updateUser() {
        System.out.println("updateUser");
        updateGoUser();
    }

    public void updateGoUser() {
        try {
            System.out.println(sqlSessionTemplate.getConnection().getAutoCommit());
            System.out.println(sqlSessionTemplate.getConnection().getTransactionIsolation());
            System.out.println(sqlSessionTemplate.getExecutorType());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        CedUserPo cedUserPos = cedUserDao.selectById(1);
        System.out.println(cedUserPos);
        // sleepMessage("第一次");

        userService2.updateGoUser(String.valueOf(new Random().nextInt(900000) + 100000));
        // sleepMessage("第二次");

        CedUserPo cedUserPos2 = cedUserDao.selectById(1);
        System.out.println(cedUserPos2);
        // sleepMessage("第三次");

        // int i = 1 / 0;
    }

    public void sleepMessage(String message) {
        System.out.println(message);
        System.out.println("开始");
        try {
            Thread.sleep(30000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("结束");
    }
}
